# -*- coding: UTF8 -*-
# Author: Sakurazaki
# (Aitor Martin. @arienai_ne - sakurazaki.setsunita@gmail.com)

import pygame

from imageloader import loadImage
import paths

class Element(object):

    def __init__(self, name, pos, texture):

        self.name = name
        self.pos = pos

        self.image = loadImage(texture)
        self.surface = self.image
        self.rect = self.surface.get_rect()
        self.rect.center = pos

        self.font = pygame.font.SysFont("Arial", 32)

        self.clicked = False
        self.hovering = False

    def update(self, delta):
        self.rect.center = self.pos

    def render(self):
        self.surface.blit(self.image, self.rect)

    def onClick(self, *args):
        self.clicked = True

    def onRelease(self):
        self.clicked = False

    def onHover(self):
        self.hovering = True

    def onFade(self):
        self.hovering = False      

class Button(Element):

    def __init__(self, text, color, pos, function):
        super(Button, self).__init__("button", pos, paths.GUITEXTURES + "button.png")

        self.function = function

        self.fontsurf = self.font.render(text, True, color)
        self.fontrect = self.fontsurf.get_rect()
        self.fontrect.center = self.rect.width /2, self.rect.height /2

        self.outsurf = pygame.surface.Surface((self.surface.get_width(), self.surface.get_height()))
        pygame.draw.rect(self.outsurf, (255, 255, 255), (15, 15, self.rect.width, self.rect.height), 3)
        self.outrect = self.outsurf.get_rect()
        self.outrect.center = self.rect.width /2, self.rect.height /2

    def update(self, delta):
        if self.clicked:
            self.clicked = False
            self.conClick()

        self.rect.center = self.pos

    def render(self):
        self.surface.blit(self.image, self.rect)
        self.surface.blit(self.fontsurf, self.fontrect)

        #if self.hovering:
            #self.surface.blit(self.outsurf, self.outrect)

    def onClick(self, *args):
        if args:
            self.function(args[0])

        else:
            self.function()

    def onHover(self):
        self.hovering = True

    def onFade(self):
        self.hovering = False
